package com.lw.chat.server.handler;

import com.lw.chat.message.ChatRequestMessage;
import com.lw.chat.message.ChatResponseMessage;
import com.lw.chat.server.session.SessionFactory;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

@ChannelHandler.Sharable
public class ChatRequestMessageHandler extends SimpleChannelInboundHandler<ChatRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ChatRequestMessage msg) throws Exception {
        String to = msg.getTo();//消息的目的地
        Channel channel = SessionFactory.getSession().getChannel(to);
        if(channel!=null){//对方在线，把消息传递给接收方
            channel.writeAndFlush(new ChatResponseMessage(msg.getFrom(), msg.getContent()));
        }else{//不在线，通知发送者对方不在线
            ctx.writeAndFlush(new ChatResponseMessage(false,"对方用户不在线"));
        }
    }
}
